package com.example.springaop.brokerdemo2;

import com.example.springaop.brokerdemo1.realsubject.RealHouseSubject;
import com.example.springaop.brokerdemo1.realsubject.RealHouseSubjectWithoutImplements;
import com.example.springaop.brokerdemo1.subject.HouseSubject;
import lombok.val;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-12-27
 * Time: 18:10
 */
//充满活力的main方法哈哈哈
public class DynamicMain {
    public static void main(String[] args) {
        /*
         * 使用cjlib代理有没有实现接口的类*/
        RealHouseSubjectWithoutImplements target = new RealHouseSubjectWithoutImplements();//创建一个类对象
        //创建一个代理对象
        RealHouseSubjectWithoutImplements  proxy = (RealHouseSubjectWithoutImplements )
                Enhancer.create(target.getClass(), new CGLIBInterceptor(target));
        proxy.rentHouse();
    }
    public static void main3(String[] args) {
        /*
        * 配置为fasle，使用jdk代理没有接口的类，代码运行失败*/
        RealHouseSubjectWithoutImplements target = new RealHouseSubjectWithoutImplements();//创建一个类对象
        //通过反射创建一个代理类，通过
        HouseSubject proxy = (HouseSubject) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                new Class[]{HouseSubject.class},
                new JDKInvocationHandler(target)
        );
        proxy.rentHouse();
    }
    public static void main2(String[] args) {
        /*
        * 使用cjlib代理有接口的类*/

        //subject可以翻译为对象,代理对象
        HouseSubject target= new RealHouseSubject();
        //创建一个代理对象
        HouseSubject proxy = (HouseSubject)
                Enhancer.create(target.getClass(), new CGLIBInterceptor(target));
        proxy.rentHouse();
    }
    public static void main1(String[] args) {
        /*
         * 配置为fasle，使用jdk代理有接口的类，运行成功*/
        HouseSubject target = new RealHouseSubject();//通过接口创建一个类对象
        //通过反射创建一个代理类，通过
        HouseSubject proxy = (HouseSubject) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                new Class[]{HouseSubject.class},
                new JDKInvocationHandler(target)
        );
        proxy.rentHouse();
    }

}
